<template>
  <div class="classroom-home">
    <div class="classroom-home-swiper">
      <el-carousel trigger="click" arrow="never"
                   :interval="3500" height="492px">
        <el-carousel-item v-for="item in swiperList" :key="item.id" class="carousel-img-wrap">
          <img :src="item.coverUrl" @click="swiperToSubject(item)" :draggable="false">
        </el-carousel-item>
      </el-carousel>
    </div>
    <h2>特色课程</h2>
    <div class="classroom-home-subject">
      <div class="classroom-home-subject-item" v-for="item in subGroupList" :key="item.menuId">
        <div class="item-title" @click="$router.push({ path: item.path })"><span>{{ item.menuName }}<img src="../assets/icons/arrow-down.png"></span></div>
        <div class="item-content">
          <div class="sub" v-for="sub in item.list" :key="sub.id"
               @click="toSubject(item, sub)" :style="{'visibility': sub.hide ? 'hidden' : ''}">
            <div class="sub-img" :style="{'background': `url(${sub.coverUrl}) no-repeat`}"></div>
            <div class="sub-text">{{ sub.videoName }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="classroom-home-bottom">
      <el-button @click="$router.push({ path: onlineStudy[0].path })">更多</el-button>
    </div>
  </div>
</template>

<script>
// eslint-disable-next-line import/no-cycle
import { getTopVideo } from '@/api/onlineClass/home'
import { cloud } from '@/framework/utils/request'
import { getOnlinePage } from '@/api/classroom/videoReview'
import { mapGetters } from 'vuex'

export default {
  name: 'ClassroomHome',
  data() {
    return {
      swiperList: [],
      subGroupList: []
    }
  },
  computed: {
    ...mapGetters([
      'onlineStudy'
    ])
  },
  watch: {
    onlineStudy: {
      immediate: true,
      deep: true,
      handler(list) {
        if (list.length > 0) {
          this.getSubjectList(list.slice(0, 3))
        }
      }
    }
  },
  mounted() {
    this.getData()
    this.$store.dispatch('classroom/changeStudentList')
  },
  methods: {
    getData() {
      this.getSwiper()
    },
    getSwiper() {
      getTopVideo().then((res) => {
        this.swiperList = res.data.map((item) => {
          const { coverId } = item
          item.coverUrl = `${process.env.VUE_APP_BASE_API}${cloud.file}/sysFileInfo/public/preview?fileId=${coverId}`
          return item
        })
      })
    },
    getSubjectList(list) {
      const requestList = []
      this.subGroupList = list.map((item) => {
        const { menuId, menuName, code, path } = item
        requestList.push(getOnlinePage({
          pageNo: 1,
          pageSize: 6,
          menuId,
          sortBy: 'DESC'
        }))
        return {
          menuId,
          menuName,
          menuCode: code,
          path,
          list: []
        }
      })
      this.$nextTick(() => {
        Promise.all(requestList).then((resList) => {
          // console.log(res)
          resList.forEach((res, i) => {
            const { data } = res
            this.subGroupList[i].list = data.rows.map((item) => {
              item.coverUrl = `${process.env.VUE_APP_BASE_API}${cloud.file}/sysFileInfo/public/preview?fileId=${item.coverId}`
              return item
            })
            if (this.subGroupList[i].list.length % 2) {
              this.subGroupList[i].list.push({ hide: true })
            }
          })
        })
      })
    },
    toSubject(groupData, subData) {
      localStorage.setItem('courseDataList', JSON.stringify(groupData.list))
      this.$router.push({
        path: '/onlineClassroom/study',
        query: {
          menuPath: groupData.path,
          vid: subData.id
        }
      })
    },
    swiperToSubject(row) {
      localStorage.setItem('courseDataList', JSON.stringify([row]))
      const routeObj = this.onlineStudy.find((item) => item.menuId == row.menuId)
      this.$router.push({
        path: '/onlineClassroom/study',
        query: {
          menuPath: routeObj.path,
          vid: row.id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.classroom-home {
  position: relative;
  width: 100%;
  height: 100%;
  &-swiper {
    position: relative;
    width: 100%;
  }
  h2 {
    font-family: "Source Han Sans CN Medium";
    font-weight: 500;
    font-size: 42px;
    text-align: center;
    color: #00080f;
    margin-block-start: 58px;
    margin-block-end: 51px;
  }
  &-subject {
    // padding: 0 226px;
    display: flex;
    justify-content: center;
    &-item {
      flex:  0 0 472px;
      width: 472px;
      height: 548px;
      background: #fff;
      box-shadow: 0 3px 6px #00000029;
      padding: 22px;
      .item-title {
        font-family: "Source Han Sans CN Medium";
        font-weight: 600;
        font-size: 20px;
        text-align: left;
        color: #00080f;
        margin-bottom: 28px;
        cursor: pointer;
        span {
          position: relative;
        }
        img {
          position: absolute;
          right: -6px;
          top: 50%;
          transform: translate(100%, -30%);
        }
      }
      .item-content {
        display: flex;
        align-items: center;
        justify-content: center;
        flex-wrap: wrap;
        .sub {
          margin-bottom: 40px;
          cursor: pointer;
          &-img {
            width: 160px;
            height: 90px;
            border-radius: 4px;
            background-color: pink;
            background-size: cover !important;
            background-position: center center !important;
            margin-bottom: 9px;
          }
          &-text {
            width: 160px;
            font-family: "Source Han Sans CN";
            font-weight: 400;
            font-size: 16px;
            text-align: left;
            color: #00080f;
            padding-left: 9px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
        .sub:nth-child(2n) {
          margin-left: 28px;
        }
      }
    }
    &-item:nth-child(2) {
      margin: 0 26px;
    }
  }
  @media screen and (min-width: 1201px) and (max-width: 1468px) {
    &-subject {
      &-item {
        flex:  0 0 400px;
        width: 400px;
        padding: 16px;
        .item-content {
          align-items: flex-start;
          justify-content: space-evenly;
          .sub:nth-child(2n) {
            margin: 0;
          }
        }
      }
    }
  }
  @media screen and (min-width: 801px) and (max-width: 1200px) {
    &-subject {
      flex-direction: column;
      align-items: center;
      &-item {
        width: 60%;
        .item-title {
          margin-bottom: 12px;
        }
        .item-content {
          justify-content: space-evenly;
          .sub {
            margin: 0 0 40px;
            &-img {
              width: 160px;
              height: 90px;
              border-radius: 4px;
              background-color: pink;
              margin: 0 30px;
              margin-bottom: 9px;
            }
            &-text {
              font-family: "Source Han Sans CN";
              font-weight: 400;
              font-size: 16px;
              text-align: left;
              color: #00080f;
              padding-left: 39px;
            }
          }
          .sub:nth-child(2n) {
            margin-left: 0;
          }
        }
      }
      &-item:nth-child(2) {
        margin: 28px 0;
      }
    }
  }
  @media screen and (max-width: 800px) {
    &-subject {
      flex-direction: column;
      align-items: center;
      &-item:nth-child(2) {
        margin: 28px 0;
      }
    }
  }
  &-bottom {
    width: 100%;
    text-align: center;
    margin: 42px 0;
    .el-button {
      width: 134px;
      height: 34px;
      background: #2d6eff;
      color: #fff;
    }
  }
  .carousel-img-wrap {
    text-align: center;
    img {
      width: 100%;
      height: 492px;
      object-fit: cover;
      cursor: pointer;
    }
  }
}
</style>
<style lang="scss">
.classroom-home {
  .el-carousel__button {
    width: 32px;
    height: 6px;
    border-radius: 1px;
    background-color: #b8babc;
  }
  .is-active {
    .el-carousel__button {
      background-color: #2d6eff;
    }
  }
  .el-carousel__container {
    margin: 0;
  }
}
</style>
